package com.agy.filemanager.controller;

import com.agy.filemanager.entity.DcBaseRole;
import com.agy.filemanager.service.DcSystemBaseRoleService;
import com.agy.filemanager.toolClass.Result;
import com.agy.filemanager.toolClass.ResultUtil;
import com.github.pagehelper.Page;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * 系统设置-角色维护
 */
@Controller
@RequestMapping("/systembaserole")
public class SystemBaseRoleController {

    @Autowired
    private DcSystemBaseRoleService dcSystemBaseRoleService;

    /**
     * 根据关键字分页查询角色表数据，关键字初始默认为空
     * @param request
     * @return
     */
    @PostMapping("/selectbaserole")
    @ResponseBody
    public Map<String,Object> selectBaseRole(HttpServletRequest request){
        int pageSize =Integer.parseInt(request.getParameter("pageSize"));
        int pageNum =Integer.parseInt(request.getParameter("pageNum"));
        String account = request.getParameter("account");
        Page list = this.dcSystemBaseRoleService.selectBaseRole(pageNum,pageSize,account);
        Map<String,Object> map=new HashMap<>();
        map.put("total",list.getTotal());
        map.put("rows",list.getResult());
        return map;
    }

    /**
     * 角色禁用与启用
     * @param request
     * @return
     */
    @PostMapping("/banbyid")
    @ResponseBody
    public Result banById(HttpServletRequest request){
        String id = request.getParameter("id");
        String a  = request.getParameter("isyes");
        Boolean isyes = Boolean.parseBoolean(a);
        dcSystemBaseRoleService.banById(id,isyes);
        return ResultUtil.success("修改成功");
    }


    /**
     * 新增角色
     * @param entity
     * @return
     */
    @PostMapping("/addbaserole")
    @ResponseBody
    public Result add(@RequestBody DcBaseRole entity){
        try {
            dcSystemBaseRoleService.addBaseRole(entity);
            return ResultUtil.success("新增成功");
        }catch(Exception ex){
            return ResultUtil.error("新增失败");
        }
    }


}
